package com.css.dzj.zfjg.modules.operatingabnormal.controller;

import com.alibaba.fastjson.JSON;
import com.css.dzj.zfjg.common.audit.service.SysAuditService;
import com.css.dzj.zfjg.common.util.Constant;
import com.css.dzj.zfjg.common.util.R;
import com.css.dzj.zfjg.modules.operatingabnormal.entity.JgLhjgOperatingAbnormalEntity;
import com.css.dzj.zfjg.modules.operatingabnormal.service.JgLhjgOperatingAbnormalService;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * 企业经营异常名录信息
 *
 * @author liyuan
 */
@RestController
@RequestMapping("generator/jglhjgoperatingabnormal")
public class JgLhjgOperatingAbnormal {
    @Autowired
    private JgLhjgOperatingAbnormalService abnormalService;

    // base日志接口
    @Autowired
    SysAuditService sysAuditService;
    @Autowired
    HttpServletRequest request;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam(required = false) String enterpriseName,
                  @RequestParam Integer pageSize, @RequestParam Integer pageNo){
        String s = "";
        try {
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults<JgLhjgOperatingAbnormalEntity> page = abnormalService.queryPage(enterpriseName, pageRequest);
            s = JSON.toJSONString(page);
            sysAuditService.saveSuccessAudit("分页查询企业经营异常名录信息成功", Constant.ZFJG_JGLHJGOPERATINGABNORMAL, Constant.PT, Constant.ZFJG_PAGE, s, request);
            return R.ok().put("page", page);
        } catch (Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("分页查询企业经营异常名录信息失败", Constant.ZFJG_JGLHJGOPERATINGABNORMAL, Constant.YZ, Constant.ZFJG_PAGE, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{operatingAbnormalId}")
    public R info(@PathVariable("operatingAbnormalId") String operatingAbnormalId){
        String s = "";
        try {
            JgLhjgOperatingAbnormalEntity jgLhjgOperatingAbnormal = abnormalService.getById(operatingAbnormalId);
            s = JSON.toJSONString(jgLhjgOperatingAbnormal);
            sysAuditService.saveSuccessAudit("通过id获取企业经营异常名录信息成功", Constant.ZFJG_JGLHJGOPERATINGABNORMAL, Constant.PT, Constant.ZFJG_SELECT, s, request);
            return R.ok().put("jgLhjgOperatingAbnormal", jgLhjgOperatingAbnormal);
        } catch (Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("通过id获取企业经营异常名录信息失败", Constant.ZFJG_JGLHJGOPERATINGABNORMAL, Constant.YZ, Constant.ZFJG_SELECT, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody JgLhjgOperatingAbnormalEntity jgLhjgOperatingAbnormal){
        String s = "";
        try {
            abnormalService.save(jgLhjgOperatingAbnormal);
            s = JSON.toJSONString(jgLhjgOperatingAbnormal);
            sysAuditService.saveSuccessAudit("企业经营异常名录信息添加成功", Constant.ZFJG_JGLHJGOPERATINGABNORMAL, Constant.PT, Constant.ZFJG_ADD, s, request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("企业经营异常名录信息添加失败", Constant.ZFJG_JGLHJGOPERATINGABNORMAL, Constant.YZ, Constant.ZFJG_ADD, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody JgLhjgOperatingAbnormalEntity jgLhjgOperatingAbnormal){
        String s = "";
        try {
            abnormalService.updateById(jgLhjgOperatingAbnormal);
            s =JSON.toJSONString(jgLhjgOperatingAbnormal);
            sysAuditService.saveSuccessAudit("企业经营异常名录信息修改成功", Constant.ZFJG_JGLHJGOPERATINGABNORMAL, Constant.PT, Constant.ZFJG_UPDATE, s, request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("企业经营异常名录信息修改失败", Constant.ZFJG_JGLHJGOPERATINGABNORMAL, Constant.YZ, Constant.ZFJG_UPDATE, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody String[] operatingAbnormalIds){
        String s = "";
        try {
            abnormalService.removeByIds(Arrays.asList(operatingAbnormalIds));
            s =JSON.toJSONString(operatingAbnormalIds);
            sysAuditService.saveSuccessAudit("企业经营异常名录信息删除成功", Constant.ZFJG_JGLHJGOPERATINGABNORMAL, Constant.PT, Constant.ZFJG_DELETE, s, request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("企业经营异常名录信息删除失败", Constant.ZFJG_JGLHJGOPERATINGABNORMAL, Constant.YZ, Constant.ZFJG_DELETE, s, e, request);
            return R.error(e.getMessage());
        }
    }

}
